package logic

import (
	"context"
	"errors"
	"work-safety/models/mysql"

	"work-safety/assrpc/assrpc"
	"work-safety/assrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LonginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLonginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LonginLogic {
	return &LonginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 用户身份验证
func (l *LonginLogic) Longin(in *assrpc.LonginRequest) (*assrpc.LonginResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Where("username = ?", in.Username).Find(&user)
	if user.ID == 0 {
		return nil, errors.New("用户未注册")
	}

	if in.Password != user.Password {
		return nil, errors.New("密码不正确")
	}

	return &assrpc.LonginResponse{
		UserId: int64(user.ID),
	}, nil
}
